package com.qys.livesMall.shop.service.impl;

import com.qys.livesMall.shop.constants.ShopConstant;
import com.qys.livesMall.shop.dao.H2ShopContractDao;
import com.qys.livesMall.shop.modal.dto.shop.contract.EditContract;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.H2ShopContract;
import com.qys.livesMall.shop.modal.vo.ShopContractVO;
import com.qys.livesMall.shop.service.H2ShopContractService;
import com.qys.livesMall.system.service.H2TemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 店铺合同 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-04-27
 */
@Service
public class MallShopContractServiceImpl implements H2ShopContractService {

    @Autowired
    private H2TemplateService templateService;

    @Autowired
    private H2ShopContractDao h2ShopContractDao;


    @Override
    public String build(H2ShopBase shop){
        String template=templateService.getByCode(ShopConstant.TemplateType.SHOP_CONTRACT);
        if (StringUtils.isBlank(template)){
            return null;
        }
        Map<String,Object> params=getParam(shop);

        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext parserContext = new TemplateParserContext();
        String shopContract = parser.parseExpression(template,parserContext).getValue(params, String.class);
        return shopContract;
    }

    private Map<String,Object> getParam(H2ShopBase shop){
        Map<String, Object> params = new HashMap<>();
        params.put("shopName", shop.getShopName());
        params.put("name", shop.getFirstName() + " " + shop.getName());
        params.put("phone", shop.getPhoneCountryCode()+" "+shop.getPhone());
        params.put("type", "其他");
//        if(Objects.equals(shop.getType(),2)) {
//            params.put("type", "直营店");
//        }

        params.put("createTime", shop.getCreateTime());
        return params;

    }

    @Override
    public H2ShopContract saveOrUp(EditContract contract){
        //检查是否已存在合同
        H2ShopContract shopContract=h2ShopContractDao.getByShopId(contract.getShopId());
        if (shopContract==null){
            shopContract=new H2ShopContract();
        }
        BeanUtils.copyProperties(contract,shopContract);
        h2ShopContractDao.saveOrUpdate(shopContract);
        return shopContract;
    }

    @Override
    public ShopContractVO getByShopId(Long shopId) {
        H2ShopContract shopContract= h2ShopContractDao.getByShopId(shopId);
        ShopContractVO vo=new ShopContractVO();
        if (shopContract==null){
            return null;
        }
        BeanUtils.copyProperties(shopContract,vo);
        return vo;

    }
}
